{extend name="layout/default" /}
{block name="head"}
<title>草莓系统登录</title>

{/block}
{block name="content"}
<div class="layui-card">
	<div id="denglu">
		<!--form表单-->
		<form class="layui-form">
		     <div class="mainBox">
            <div class="main-container">

			<div class="layui-form-item">
				<label class="layui-form-label">手机号</label>
				<div class="layui-input-block">
					<input type="text" name="username" placeholder="请输入手机号" autocomplete="off" class="layui-input"
						lay-verify="required">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">密码</label>
				<div class="layui-input-block">
					<input type="password" name="password" placeholder="请输入密码" autocomplete="off" class="layui-input"
						lay-verify="required">
				</div>
			</div>

			<div class="layui-form-item">
				<div class="layui-input-block">
					<button class="layui-btn" lay-submit lay-filter="add">立即登录</button>
					<button type="reset" class="layui-btn layui-btn-primary" id="quzhuce">没账户去注册</button>
				</div>
			</div>
			
			</div>
			</div>
		</form>
	</div>


	<!-- 注册 -->
	<div id="zhuce" style="display: none;">
		<!--form表单-->
		<form class="layui-form">
		    <div class="mainBox">
            <div class="main-container">

			<div class="layui-form-item">
				<label class="layui-form-label">手机号</label>
				<div class="layui-input-block">
					<input type="text" name="username" placeholder="请输入手机号" autocomplete="off" class="layui-input"
						lay-verify="required">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">密码</label>
				<div class="layui-input-block">
					<input type="password" name="password" placeholder="请输入密码" autocomplete="off" class="layui-input"
						lay-verify="required">
				</div>
			</div>

			<div class="layui-form-item">
				<div class="layui-input-block">
					<button class="layui-btn layui-btn-primary" lay-submit id="qudenglu">有账户去登录</button>
					<button lay-submit lay-filter="reg" class="layui-btn">立即注册</button>
				</div>
			</div>
				</div>
			</div>
		</form>
	</div>
</div>
{/block}
{block name="scripts"}

<!--js逻辑-->
<script src="/static/js/jquery.min.js"></script>
<script>
	$("#quzhuce").click(function () {
		$("#zhuce").show(100);
		$("#denglu").hide(100);
	});

	$("#qudenglu").click(function () {
		$("#zhuce").hide(100);
		$("#denglu").show(100);
	});

</script>
<script>

	layui.use(["jquery", "form"], function () {
		let form = layui.form;
		let $ = layui.jquery;

		//laydate.render({elem: "#birthday", type: "datetime"});

		form.verify({

		});

		form.on("submit(add)", function (data) {
			$.ajax(
			    
			    {
				
			            	url:'https://www.caomei.zone/api/user/login',
                            data:data.field,
                            //dataType: 'json',
                            type: 'post',
				
				}
			    
			    //"https://www.caomei.zone/api/user/login", "post", data.field, true
			
			
			
			).done(function (response) {
				//console.log(response);
				if (response.code == 1) {
					layer.msg('登录成功', {
                                        icon: 1,
                                        time: 1000
                                    }, function () {

						const Storage = {};
						Storage.get = function (name) {
							return localStorage.getItem(name);
						}

						Storage.set = function (name, val) {
							localStorage.setItem(name, val);
						}

						Storage.set("caomeiadmin_userinfo", response.data.token);
						console.log(Storage.get("caomeiadmin_userinfo"));

						parent.layer.close(parent.layer.getFrameIndex(window.name));
					});
				} else {
					layer.msg(response.msg, {
                                        icon: 2,
                                        time: 1000
                                    }, function () {

					})
				}

			}).fail(function (error) {
				console.log(error)
			});
			return false;
		});


		//注册
		form.on("submit(reg)", function (data) {
			$.ajax(
			    
			    
			      {
				
			            	url: "https://www.caomei.zone/api/user/reg",
                            data:data.field,
                            //dataType: 'json',
                            type: 'post',
				
				}
			    
			    ).done(function (response) {
				//console.log(response);
				if (response.code == 1) {
					layer.msg('注册成功', {
                                        icon: 1,
                                        time: 1000
                                    }, function () {

						const Storage = {};
						Storage.get = function (name) {
							return localStorage.getItem(name);
						}

						Storage.set = function (name, val) {
							localStorage.setItem(name, val);
						}

						Storage.set("caomeiadmin_userinfo", response.data.token);
						console.log(Storage.get("caomeiadmin_userinfo"));

						parent.layer.close(parent.layer.getFrameIndex(window.name));
					});
				} else {
						layer.msg(response.msg, {
                                        icon: 2,
                                        time: 1000
                                    }, function () {

					})
				}

			}).fail(function (error) {
				console.log(error)
			});
			return false;
		});


	});
</script>
{/block}